﻿using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class ObjectPoolManager : MonoBehaviour {
    public static ObjectPoolManager instance;
    private int MaxCount = 5;//最大容量
    public GameObject soldier;
    public GameObject archer;
    private List<GameObject> soldierList;//士兵池
    private List<GameObject> archerList;//弓兵池

    public bool lockPoolSize = false;//是否锁定子弹池大小
    private int SoldiercurrentIndex = 0;//当前指向链表位置索引
    private int ArchercurrentIndex = 0;//当前指向链表位置索引

    void Awake()
    {
        instance = this;
    }
    // Use this for initialization
    void Start()
    {
        soldierList = new List<GameObject>();
        archerList = new List<GameObject>();
        for (int i = 0; i < MaxCount; i++)
        {

            GameObject tempsoldier = Instantiate(Resources.Load("Prefabs/Soldier")) as GameObject;
            tempsoldier.SetActive(false);
            soldierList.Add(tempsoldier);
            //GameObject temparcher = Instantiate(Resources.Load("Prefabs/ArcherSoldier")) as GameObject;
            //temparcher.SetActive(false);
            //archerList.Add(temparcher);
        }
        //Debug.Log(soldierList.Count);
    }

    public GameObject GetPoolObject(string ObjectName)
    {
        if (ObjectName == "Soldier")
        {
            for (int i = 0; i < soldierList.Count; i++)
            {
                //int temp = (SoldiercurrentIndex + i) & MaxCount;
                //if (!soldierList[temp].activeInHierarchy)
                //{
                //    SoldiercurrentIndex = (temp + 1) % soldierList.Count;
                //    return soldierList[temp];
                //}
                if (!soldierList[i].activeInHierarchy)
                {
                    //soldierList[i].GetComponent<Soldier>().hpSlider.SetActive(true);
                    soldierList[i].GetComponent<Soldier>().currentHP = 100;
                    if (soldierList[i].GetComponent<Soldier>().reTrBloodThis != null)
                    {
                        soldierList[i].GetComponent<Soldier>().reTrBloodThis.gameObject.SetActive(true);
                    }
                    return soldierList[i];
                }
            }
            ////如果遍历完一遍子弹库发现没有可以用的，执行下面
            //if (!lockPoolSize)//如果没有锁定对象池大小，创建子弹并添加到对象池中。
            //{
            //    GameObject obj = Instantiate(Resources.Load("Prefabs/Soldier")) as GameObject;
            //    soldierList.Add(obj);
            //    return obj;
            //}
        }
        //else if (ObjectName == "ArcherSoldier")
        //{
        //    for (int i = 0; i < archerList.Count; i++)
        //    {
        //        //int temp = (ArchercurrentIndex + i) & MaxCount;
        //        //if (!archerList[temp].activeInHierarchy)
        //        //{
        //        //    ArchercurrentIndex = (temp + 1) % archerList.Count;
        //        //    return archerList[temp];
        //        //}
        //        if (!archerList[i].activeInHierarchy)
        //        {
        //            if (archerList[i].GetComponent<ArcherSoldier>().reTrBloodThis != null)
        //            {
        //                archerList[i].GetComponent<ArcherSoldier>().reTrBloodThis.gameObject.SetActive(true);
        //            }
        //            archerList[i].GetComponent<ArcherSoldier>().hp = 100;
        //            return archerList[i];
        //        }
        //    }
        //    //如果遍历完一遍子弹库发现没有可以用的，执行下面
        //    if (!lockPoolSize)//如果没有锁定对象池大小，创建子弹并添加到对象池中。
        //    {
        //        GameObject obj = Instantiate(Resources.Load("Prefabs/ArcherSoldier")) as GameObject;
        //        archerList.Add(obj);
        //        return obj;
        //    }
        //}
        return null;
    }
	
	// Update is called once per frame
	void Update () {
		
	}
}
